/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.generator.builder;

/**
 *
 * @author 45581126
 */
public class Builder {
    private String folderProject;
    private String folderSrc;
    private String folderBuild;
    private String folderBin;
    private boolean debug;
    private javax.tools.StandardJavaFileManager fileManager;
    private java.util.LinkedList<String> libraries;

    public void addLibrary(String file) { libraries.add(file); }

    public Builder(String projectFolder, boolean debug) {
        this.libraries = new java.util.LinkedList<String>();
        this.folderProject = projectFolder;
        this.folderSrc = folderProject + "src/";
        this.folderBuild = folderProject + "build/";
        this.folderBin = folderProject + "dist/";
        this.debug = debug;
        this.fileManager = javax.tools.ToolProvider.getSystemJavaCompiler().getStandardFileManager(new javax.tools.DiagnosticCollector<javax.tools.JavaFileObject>(), null, null);
    }

    public void build(String jarName) throws java.io.IOException, java.lang.InterruptedException {
        java.io.File dir = new java.io.File(folderBuild);
        if (dir.exists())
            deleteDirectory(dir);
        dir.mkdir();

        java.lang.System.out.println("-- Compiling");
        java.io.OutputStreamWriter writer = new java.io.OutputStreamWriter(java.lang.System.out, java.nio.charset.Charset.forName("UTF-8"));
        java.util.LinkedList<String> options = build_JavaCParams();
        javax.tools.JavaCompiler.CompilationTask task = javax.tools.ToolProvider.getSystemJavaCompiler().getTask(writer, fileManager, null, options, null, fileManager.getJavaFileObjectsFromFiles(build_CodeList()));
        task.call();

        dir = new java.io.File(folderBin);
        if (dir.exists())
            deleteDirectory(dir);
        dir.mkdir();
        
        java.lang.System.out.println("-- Creating JAR");
        String jarClassPath = build_GetClassPath();
        JarManager.makeJar(folderBuild, folderBin + jarName + ".jar", "1.0", null, jarClassPath);
    }

    private java.util.LinkedList<java.io.File> build_CodeList() throws java.io.IOException {
        java.util.LinkedList<java.io.File> sources = new java.util.LinkedList<java.io.File>();
        build_CodeList_Directory(sources, new java.io.File(folderSrc));
        return sources;
    }
    private void build_CodeList_Directory(java.util.LinkedList<java.io.File> sources, java.io.File dir) throws java.io.IOException {
        for (java.io.File element : dir.listFiles()) {
            if (element.isDirectory())
                build_CodeList_Directory(sources, element);
            else if (element.getName().endsWith(".java"))
                sources.add(element);
        }
    }
    private java.util.LinkedList<String> build_JavaCParams() throws java.io.IOException {
        java.util.LinkedList<String> options = new java.util.LinkedList<String>();
        if (debug) options.add("-g");
        else options.add("-g:none");
        options.add("-d");
        options.add(folderBuild);
        options.add("-encoding");
        options.add("UTF-8");
        if (libraries.size() != 0) {
            options.add("-classpath");
            String classpath = "";
            for (int i=0; i!=libraries.size(); i++) {
                if (i!=0) classpath += ";";
                classpath += libraries.get(i);
            }
            options.add(classpath);
        }
        return options;
    }
    private String build_GetClassPath() {
        java.lang.StringBuilder buffer = new java.lang.StringBuilder();
        for (int i=0; i!=libraries.size(); i++) {
            java.io.File file = new java.io.File(libraries.get(i));
            if (i!=0) buffer.append(";");
            buffer.append("lib/");
            buffer.append(file.getName());
        }
        return buffer.toString();
    }
    
    
    private void deleteDirectory(java.io.File dir) {
        java.io.File[] children = dir.listFiles();
        if (children == null)
            return;
        for (int i=0; i!=children.length; i++) {
            if (children[i].isDirectory())
                deleteDirectory(children[i]);
            else
                if (!children[i].delete()) {
                    java.lang.System.out.println("failed to delete: " + children[i].toString());
                }
        }
        dir.delete();
    }
}
